package com.wuzheng.wisgo.sanitation.cloud.service.facility.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wuzheng.wisgo.sanitation.cloud.constant.Constants;
import com.wuzheng.wisgo.sanitation.cloud.constant.ErrorConst;
import com.wuzheng.wisgo.sanitation.cloud.enums.InfixDataEnum;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.param.FacilityDetailParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.param.FacilityListParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.param.TrashStateParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.pojo.FacilityDTO;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.pojo.TrashStateDTO;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.facility.Facility;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.picture.Picture;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.transfer.TransferRouteTrash;
import com.wuzheng.wisgo.sanitation.cloud.repository.mapper.facility.FacilityMapper;
import com.wuzheng.wisgo.sanitation.cloud.service.facility.FacilityService;
import com.wuzheng.wisgo.sanitation.cloud.service.picture.PictureService;
import com.wuzheng.wisgo.sanitation.cloud.service.transfer.TransferRouteTrashService;
import com.wuzheng.wisgo.sanitation.cloud.utils.MethodUtils;
import com.wuzheng.wisgo.sanitation.framework.base.service.impl.BaseServiceImpl;
import com.wuzheng.wisgo.sanitation.framework.core.constant.SysRespEnum;
import com.wuzheng.wisgo.sanitation.framework.core.util.ServiceAssert;
import com.wuzheng.wisgo.sanitation.framework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author：hoipang
 * @date：Created in 2019-12-03
 * @description：环卫设施信息服务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class FacilityServiceImpl extends BaseServiceImpl<Facility> implements FacilityService {

    @Resource
    TransferRouteTrashService transferRouteTrashService;
    @Resource
    PictureService pictureService;

    @Override
    public List<FacilityDTO> listFacilityDTOs(FacilityListParam facilityListParam) {
        FacilityMapper facilityMapper = this.getMapper(FacilityMapper.class);
        List<FacilityDTO> list;

        if (facilityListParam.getPageSize() != null && facilityListParam.getPageIndex() != null) {
            Page<FacilityDTO> page = new Page<>(facilityListParam.getPageIndex(), facilityListParam.getPageSize());
            list = facilityMapper.pageFacilityDTOs(page, Constants.EMPYT_STRING, facilityListParam.getFacName(), facilityListParam.getFacType());

            return list;
        }
        list = facilityMapper.listFacilityDTOs(Constants.EMPYT_STRING, facilityListParam.getFacName(), facilityListParam.getFacType());

        return list;
    }

    @Override
    public List<TrashStateDTO> listTrashStateDTOs(TrashStateParam trashStateParam) {
        FacilityMapper facilityMapper = this.getMapper(FacilityMapper.class);
        List<TrashStateDTO> trashStateDTOList = new ArrayList<>();
        switch (trashStateParam.getType()) {
            case Constants.TrashRequestType.TRANSFER_ROUTE:
                trashStateDTOList = facilityMapper.listTrashDistributionStateDTOs();
                break;
            default:
                trashStateDTOList = facilityMapper.listTrashClearStateDTOs();
                break;
        }

        return trashStateDTOList;
    }

    @Override
    public void removeFacilities(List<String> list) {
        ServiceAssert.isTrue(list != null && !list.isEmpty(), SysRespEnum.ILLEGAL_ARGUMENT.getCode(),
                ErrorConst.Common.ARGUMENT_NOT_NULL);
        List<TransferRouteTrash> relationList = transferRouteTrashService.selectList(list);
        ServiceAssert.isTrue(relationList == null || relationList.isEmpty(), SysRespEnum.SERVICE_EXCEPTION.getCode(),
                ErrorConst.Common.REMOVE_RELATION);
        List<Facility> facilityList = new ArrayList<>();
        list.stream().forEach(id -> {
            Facility facility = new Facility();
            facility.setId(id);
            facility.setDeFlag(true);
            facilityList.add(facility);
        });
        this.updateBatchById(facilityList, facilityList.size());
    }

    @Override
    public FacilityDTO selectFacilityDTO(String id) {
        ServiceAssert.isTrue(StringUtils.isNotBlank(id), SysRespEnum.ILLEGAL_ARGUMENT.getCode(),
                ErrorConst.Common.ARGUMENT_NOT_NULL);
        FacilityMapper facilityMapper = this.getMapper(FacilityMapper.class);
        FacilityDTO facilityDTO = facilityMapper.selectFacilityDTO(id, Constants.EMPYT_STRING, Constants.EMPYT_STRING);
        ServiceAssert.isTrue(facilityDTO != null, SysRespEnum.SERVICE_EXCEPTION.getCode(),
                ErrorConst.Common.QUERY_RESULT_NOT_EXIST);
        return facilityDTO;
    }

    @Override
    public FacilityDTO insertOrUpdate(User user, FacilityDetailParam facilityDetailParam) {
        Facility facility = new Facility();
        facility.setCustomerId(facilityDetailParam.getCustomerId());
        facility.setFacilityName(facilityDetailParam.getFacName());
        facility.setFacilityTypeId(facilityDetailParam.getFacType());
        facility.setFacilityType(facilityDetailParam.getFacTypeName());
        facility.setDeptName(facilityDetailParam.getDeptName());
        facility.setAddress(facilityDetailParam.getAddress());
        if (StringUtils.isNotBlank(facilityDetailParam.getLongitude())) {
            facility.setLongitude(Double.valueOf(facilityDetailParam.getLongitude()));
        }
        if (StringUtils.isNotBlank(facilityDetailParam.getLatitude())) {
            facility.setLatitude(Double.valueOf(facilityDetailParam.getLatitude()));
        }
        facility.setRemark(facilityDetailParam.getRemark());

        String method = "";
        if (StringUtils.isNotBlank(facilityDetailParam.getMethod())) {
            method = facilityDetailParam.getMethod().toLowerCase();
        }
        Boolean result = MethodUtils.allowed(method, facilityDetailParam.getId());
        ServiceAssert.isTrue(result, SysRespEnum.ILLEGAL_ARGUMENT.getCode(), SysRespEnum.ILLEGAL_ARGUMENT.getMessage());

        if (InfixDataEnum.ADD.getTypeId().equals(facilityDetailParam.getMethod())) {
            facility.setDeFlag(false);
            facility.setCreator(user.getName());
            facility.setCreateTime(new Date());
            this.insert(facility);
        }

        String facilityId = StringUtils.isBlank(facilityDetailParam.getId()) ? facility.getId() : facilityDetailParam.getId();

        if (InfixDataEnum.UPDATE.getTypeId().equals(facilityDetailParam.getMethod())) {
            facility.setId(facilityId);
            facility.setModifier(user.getName());
            facility.setModifyTime(new Date());
            this.updateById(facility);
        }
        String imageId = facilityDetailParam.getImageId();
        switch (InfixDataEnum.getByTypeCode(method)) {
            case UPDATE:
                Wrapper<Picture> wrapper = new EntityWrapper<>();
                wrapper.eq("peference_id", facilityId).eq("de_flag",false);
                List<Picture> list = pictureService.selectList(wrapper);
                Boolean flag = true;
                for (Picture item : list) {
                    if (imageId.equals(item.getId())) {
                        flag = false;
                    } else {
                        item.setDeFlag(true);

                    }
                }
                if (StringUtils.isNotBlank(imageId) && flag) {
                    pictureService.updateBatchById(list, list.size());
                    Picture picture = pictureService.selectById(imageId);
                    picture.setPeferenceId(facilityId);
                    pictureService.updateById(picture);
                }
                break;
            case ADD:
                if (StringUtils.isNotBlank(imageId)) {
                    Picture picture = pictureService.selectById(imageId);
                    picture.setPeferenceId(facilityId);
                    pictureService.updateById(picture);
                }
                break;
        }
        FacilityDTO facilityDetailDTO = this.selectFacilityDTO(facilityId);
        return facilityDetailDTO;
    }
}
